import random
ch = int(input('Введите четырёхзначное число: '))
max = 0
if ch > 9999 or ch < 1000:
    print ('0')
def prost(n):
    if n <= 1:
        return False
    for _ in range(5):
        a = random.randint(1, n-1)
        if pow(a, n-1, n) != 1:
            return False
        return True 


t = ch // 1000
if prost(t) == True:
    max = t#1


ts = ch // 100
if prost(ts) == True and ts > max:
    max = ts#2


tsd = ch // 10
if prost(tsd) == True and tsd > max:
    max = tsd#3


tsde = ch
if prost(tsde) == True and tsde > max:
    max = tsde#4


s = ch // 100 % 10
if prost(s) == True and s > max:
    max = s#5


sd = ch // 10 % 100
if prost(sd) == True and sd > max:
    max = sd#6


sde = ch % 1000
if prost(sde) == True and sde > max:
    max = sde#7


d = ch // 10 %10
if prost(d) == True and d > max:
    max = d#8


de = ch % 100
if prost(de) == True and de > max:
    max = de#9


e = ch % 10
if prost(e) == True and e > max:
    max = e#10


print(max)